package com.itheima.crm.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.io.*;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.itheima.common.utils.Page;
import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.RiZhi;
import com.itheima.crm.pojo.YuanGong;
import com.itheima.crm.service.BaseDictService;
import com.itheima.crm.service.OrganizationService;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class AddresslistController {
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private RiZhiService rizhiService;
	
	public BaseDictService getBaseDictService() {
		return baseDictService;
	}
	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}
	public YuanGongService getYuangongService() {
		return yuangongService;
	}
	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}
	public OrganizationService getOrganizationService() {
		return organizationService;
	}
	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}
	public RiZhiService getRizhiService() {
		return rizhiService;
	}
	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}
	@RequestMapping(value = "/Addresslist")
	public String list(YuanGong vo, Model model) {

		List<BaseDict> SexType = baseDictService.selectBaseDictListByCode("001");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> zhiweiType = baseDictService.selectBaseDictListByCode("003");

		model.addAttribute("SexType", SexType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("zhiweiType", zhiweiType);

		// 通过四个条件 查询分页对象

		Page<YuanGong> page = yuangongService.selectPageByQueryVo(vo);

		model.addAttribute("page", page);

		return "Addresslist";
	}
	@RequestMapping(value = "/Addresslistmain")
	public ModelAndView listmain(HttpServletRequest request,YuanGong vo, Model model) {

		System.out.println(vo.getId());	
			ModelAndView a = new ModelAndView("Addresslistmain");
		YuanGong yuangong=yuangongService.selectYuanGongById(vo.getId());
	System.out.println(yuangong.getPic());
		a.addObject("yuangong",yuangong);
		
		

		return a;
	}
	@RequestMapping(value = "/updatepic.action")
	public String pic(YuanGong vo,MultipartFile pictureFile) throws IllegalStateException, IOException {
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
		pictureFile.transferTo(new File("D:\\q\\" + name + "." + ext));

		vo.setPic(name + "." + ext);
		System.out.println(vo.getPic());
		System.out.println(vo.getId());
		YuanGong yuangong=new YuanGong();
		yuangong.setId(vo.getId());
		yuangong.setPic(vo.getPic());
		yuangongService.updatePic(yuangong);
		  RiZhi c=new RiZhi();
			
			c.setYgid(yuangong.getYgid());
			c.setYgname(yuangong.getYgname());
			c.setInsertdate(new Date());
			System.out.println(c.getInsertdate());
			c.setDetial("上传员工照片");
			rizhiService.insertApplication(c);
		return "redirect:/Addresslistmain?id="+vo.getId();

		
	}
	
	
	
	
	
	

}
